package task3.source;

import java.io.StreamCorruptedException;
import javax.media.jai.JAI;
import task3.pimpmypipe.AbstractSource;
import task3.util.FilterObservable;
import task3.util.ImageTO;

public class Source
  extends AbstractSource<ImageTO>
{
  private String _fileName;
  private FilterObservable _observable;
  
  public Source()
  {
    this._fileName = " ";
    this._observable = new FilterObservable();
  }
  
  public ImageTO read()
  {
    ImageTO image = new ImageTO();
    
    image.setSourceImage(JAI.create("fileload", this._fileName));
    image.setEditedImage(image.getSourceImage());
    image.setFileName(this._fileName);
    
    this._observable.setChanged();
    this._observable.notifyObservers(image);
    
    return image;
  }
  
  public void run()
  {
    try
    {
      writeOutput(read());
    }
    catch (StreamCorruptedException e)
    {
      e.printStackTrace();
    }
  }
  
  public void setFileName(String fileName)
  {
    this._fileName = fileName;
  }
  
  public String getFileName()
  {
    return this._fileName;
  }
  
  public FilterObservable getObservable()
  {
    return this._observable;
  }
}
